﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlickrDownloadr.Services;
using FlickrNet;

namespace FlickrDownloadr
{
    public partial class GroupOptions : UserControl, IWizardPage
    {
        public GroupOptions()
        {
            InitializeComponent();
        }

        public void ReloadPage()
        { }

        public Bitmap Icon
        {
            get { return Properties.Resources._1403_Globe; }
        }

        public event NextDelegate Next;

        public void OnNext(bool refresh)
        {
            if (Next != null)
                Next(this, false);
        }

        public SearchOptions SearchOptions { get; set; }

        public string Title
        {
            get { return Properties.Resources.PageGroupTitle; }
        }
        public string Description
        {
            get { return Properties.Resources.PageGroupDescription; }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            PhotoService.Instance.SearchOptions.Type = SearchType.Group;

            GroupSearchResult result = (GroupSearchResult)listBox1.SelectedItem;

            PhotoService.Instance.SearchOptions.Text = result.GroupName;// textBox1.Text.Trim();
            PhotoService.Instance.Options.GroupId = result.GroupId;
            OnNext(true);
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text.Trim();
            var groups = FlickrFactory.GetInstance().GroupsSearch(text);

            listBox1.DisplayMember = "GroupName";
            listBox1.ValueMember = "GroupId";
            listBox1.DataSource = groups.Groups;

            buttonNext.Enabled = (groups.Groups.Count > 0);
        }

        private void GroupOptions_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                buttonSearch.PerformClick();
            }
        }
    }
}
